package mobile.pubcrawler.ui;

import mobile.pubcrawler.Filter;
import mobile.pubcrawler.FooterView;
import mobile.pubcrawler.MyListAdapter;
import mobile.pubcrawler.PubCrawlerUI;
import mobile.pubcrawler.PubListView;
import mobile.pubcrawler.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class NextPub extends Activity {
    
	private ProgressBar progress;
	private FrameLayout progressFrame;
	private PubListView myList;
	private int notAvailableSpaceVertical = 95;
	private FooterView footer;
	private MyListAdapter myListAdapter;
	private TextView noLocationFound;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	
        setContentView(R.layout.nextpub);
        setTitle(R.string.nextPubDataLoading);
        
     	Filter.getInstance().setAddressChanged(false);	
    	
        progressFrame = (FrameLayout)findViewById(R.id.progressBarFrame);
    	progress = (ProgressBar)findViewById(R.id.progressBar);
    	progress.setMax(100);
    		
    	myListAdapter = new MyListAdapter(this,1);
    	myList = (PubListView)findViewById(R.id.nextPubListView);
    	myList.setNotAvailableSpaceVertical(notAvailableSpaceVertical);
        myList.setAdapter(myListAdapter);
        myList.setOnItemClickListener(PubCrawlerUI.getInstance().onClickPubListener);
    	
        footer = (FooterView)this.findViewById(R.id.nextPubFooterView);
    	footer.setGravity(Gravity.CENTER_HORIZONTAL);
    	footer.setOnClickListener(footerClickListener);
    	
    	noLocationFound = (TextView)findViewById(R.id.nextPubNoLocationFound);
    }
    
    public void onResume() {
		super.onResume();
		if (Filter.getInstance().isAddressChanged()) {
			noLocationFound.setVisibility(View.GONE);
			setTitle(R.string.nextPubDataLoading);
			myListAdapter.loadLocations();
			Filter.getInstance().setAddressChanged(false);
		}
		if (footer!=null) footer.update();
	}
    
    public void setProgressBar(int p) {
    	if (progress.getVisibility()==View.GONE) showProgressBar();
    	progress.setProgress(p);
    }
    
    public void hideProgressBar() {
    	progress.setVisibility(View.GONE);
    	myList.setNotAvailableSpaceVertical(notAvailableSpaceVertical-progressFrame.getHeight());
    }
    
   public void showProgressBar() {
    	progress.setVisibility(View.VISIBLE);
    	myList.setNotAvailableSpaceVertical(notAvailableSpaceVertical);
    }
    
    public void setHeadline(String t) {
    	setTitle(t);
    }
    
    private OnClickListener footerClickListener =  new OnClickListener(){
        public void onClick(View v) { PubCrawlerUI.getInstance().startStartLocationUI(); }
    };
    
    public void showNoLocationMessage() {
    	noLocationFound.setVisibility(View.VISIBLE);
    	myList.setNotAvailableSpaceVertical(myList.getNotAvailableSpaceVertical()+20);
    }
    
    //----------------- Menu inflaters and listeners -------------------
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
    }
    
    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menuMainMenu:
        	PubCrawlerUI.getInstance().startMainUI();
            return true;
        case R.id.menuSettings:
        	PubCrawlerUI.getInstance().startSettingsUI();
            return true;
        case R.id.menuQuit:
            System.exit(0);
            return true;
        }
        return false;
    }
    
}
